<?php 
class PublicAction extends Action{

	public function init(){
		$userInfo = $this->user;
		$cardId =  $userInfo['uname'];
		if(!$cardId) $this->error('你还未登陆');
		$count	=	M('Space')->where("uid='$_SESSION[mid]'")->find();
		if(!$count){		
			if($_POST){
				$pBaseInfo = D('BaseInfo');
				$hrbiInfo = $pBaseInfo->get_Hrbi_info( $cardId );
				if( $hrbiInfo ){
					$list = $pBaseInfo ->get_Hrbi_Info_Data( $cardId ,'baseinfo' );
					foreach($list as $key=>$value){
						$data[$key] = $value['value'];
					}
				}else{
					$data = $_POST['data'];
				}
				
				if($result = D('BaseInfo')->init_Base_Info($this->mid,$cardId,$data)){
					
					$this->_addFollowUser();
					
					/**
					if(x('Invite')->checkCode($_POST['invitecode'],'colleague',$cardId)){
						$this->doinvite($this->mid,$_POST['invitecode']);
					}
					
					if(x('Invite')->checkCode($_POST['invitecode'],'group',$cardId)){
						$info = x('Invite')->getData( $_POST['invitecode'] );
						redirect( U('group/Member/invitejoin',array('gid'=>$info['data']['gid'],'invitecode'=>$_POST['invitecode'])) );
					} **/

					$this->checkInvite( $_POST['invitecode'] , $cardId );  //执行邀请动作
					
					redirect('index.php?app=home&mod=user');
				}
				unset($_SESSION['NEED_ACTIVE']);
			}
			//$cardId =  '00135495';
			$list		=	D('BaseInfo')->get_Hrbi_Info_Data($cardId);
			$hrbiInfo	=	D('BaseInfo')->get_Hrbi_info($cardId);
			$this->assign($this->user);
			$this->assign('isHrbi',$hrbiInfo);
			$this->assign('list',$list);
			$this->assign('invitecode',$_GET['invitecode']);
			$this->display();
		}else{
			unset($_SESSION['NEED_ACTIVE']);
			redirect('index.php?app=home&mod=user');
		}
	}
	
	public function login(){
		if( $this->mid > 0 ){
			U('home','',true);	
		}
		$this->display();
	}

	//执行登录动作
	public function dologin() {

		$username	=	$_POST['username'];
		$password	=	$_POST['password'];
		$refer_url	=	$_POST['refer'];
		$passport	=	service('Passport','LocalAuth');
		$passport->method = 'LocalAuth';
		$result	=	$passport->authentication($username,$password);

		if($result){
			if($refer_url!=''){
				unset($_SESSION['refer_url']);
			}else{
				$refer_url = SITE_URL;
			}
			$this->assign('jumpUrl',$refer_url);
			redirect(SITE_URL);
			//$this->success($username.' 登录成功！');
		}else{
			$this->error('登录失败！');
		}
	}
	
	//退出登录
	public function logout() {

		$username	=	$_SESSION['uname'];

		$passport	=	service('Passport');
		$passport->logoutLocal($username);
		redirect(SITE_URL);
	}

	function help(){
		$this->display();
	}	
	
	//关注
	private function _addFollowUser(){
		$dao = D('Follow','home');
		$mid = $this->mid;
		$uid = 1;
		if($mid==$uid) return false;
		
		$map['type'] = 'user';
		//我关注小Hi
		$map['uid']  = $mid;
		$map['aimId'] = $uid;	
		$map['data'] = $uid;		
		$dao->add($map);
		
		$map['uid']  = $uid;
		$map['aimId'] = $mid;	
		$map['data'] = $mid;		
		$dao->add($map);		
	}
	
	//执行邀请动作
	private function checkInvite( $code ){
		$info = x('Invite')->getData($code);
		if($info['touser']!=$this->mid && $info['touser']!=$this->user['uname'] ) return '';
		
		switch($info['type']){
			case 'colleague':
				$this->doinvite( $this->mid,$info );
			break;
			
			case 'group':
				redirect( U('group/Member/invitejoin',array('gid'=>$info['data']['gid'],'invitecode'=>$_POST['invitecode'])) );
			break;
		}
	}
	
	//受邀请激活
	private function doinvite($uid,$info){
		if($info){
			D('Colleague')->doaddOne($info['uid'],$uid,$info['data']['gid'],$info['data']['remark'],$info['data']['attention']);
		}
	}
	
	function search(){
		$q = t( $_REQUEST['q'] );
		$list = M('search_key')->field('`key`,`searchCount`')->where("suggest=0 AND `key` LIKE '".$q."%'")->order('searchCount Desc')->findall();
		$searcharray = array();
		if( $list ){
			foreach($list as $key=>$value){
				$searcharray[$key]['key']   = $value['key'];
				$searcharray[$key]['count'] = $value['searchCount'];
			}
		}
		exit(json_encode($searcharray));
	}
}
?>